﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SmartMD.Manager.SysInfo;
using System.ComponentModel;
using SmartMD.DataModel;
using Techstar.Dialog;
using Techstar.Progress;
using SmartMD.DataAccess.Service;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Interface.Dao;

namespace SmartMD.Manager.Common
{
    /// <summary>
    /// Interaction logic for CommitDialog.xaml
    /// </summary>
    public partial class CommitDialog : Window
    {
        private IBaseGenericDao _service;
        private BaseWindow _owner;
        private readonly ObservableCollection<DictionaryType> _dictionaryTypes;
        private readonly ObservableCollection<SpecEntity> _containerEntitys;
        private readonly ObservableCollection<ExamItemInfo> _examItemInfos;

        public CommitDialog(ObservableCollection<DictionaryType> items)
        {
            InitializeComponent();
            _dictionaryTypes = items;
            var view = CollectionViewSource.GetDefaultView(_dictionaryTypes);
            view.GroupDescriptions.Add(new PropertyGroupDescription("DictionaryType"));
            view.SortDescriptions.Add(new SortDescription("DictionaryType", ListSortDirection.Ascending));
            view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
            lbFrameList.ItemsSource = view;
            this.Title = "字典维护提交";
        }

        public CommitDialog(ObservableCollection<SpecEntity> items)
        {
            InitializeComponent();
            _containerEntitys = items;
            var view = CollectionViewSource.GetDefaultView(_containerEntitys);
            view.GroupDescriptions.Add(new PropertyGroupDescription("ContainerEntity"));
            view.SortDescriptions.Add(new SortDescription("ContainerEntity", ListSortDirection.Ascending));
            view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
            lbFrameList.ItemsSource = view;
            this.Title = "容器维护提交";
        }
        public CommitDialog(ObservableCollection<ExamItemInfo> items, IBaseGenericDao service, BaseWindow owner)
        {          
            InitializeComponent();
            _service = service;
            _examItemInfos = items;
            _owner = owner;
            var view = CollectionViewSource.GetDefaultView(_examItemInfos);
            view.GroupDescriptions.Add(new PropertyGroupDescription("ExamItemInfos"));
            view.SortDescriptions.Add(new SortDescription("ExamItemInfos", ListSortDirection.Ascending));
            view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
            lbFrameList.ItemsSource = view;
            this.Title = "检查项目提交";
        }
       public CommitDialog(ObservableCollection<ExamItemValue> items, IExamItemValueDao service, BaseWindow owner)
       {
           
       }

        private void BtnOkClick(object sender, RoutedEventArgs e)
        {
            var result = new List<CacheEntity>();
            if (_dictionaryTypes != null)
            {
                var items = from item in _dictionaryTypes where item.IsSelected && item.State != RecordState.Normal select item;
                result.AddRange(items);
            }
            if (_examItemInfos != null)
            {
                var items = from item in _examItemInfos where item.IsSelected && item.State != RecordState.Normal select item;
                result.AddRange(items);
            }

            if (result.Count == 0)
            {
                TaskDialogEx.Warning("没有可以提交到数据服务器的内容!");
                return;
            }
           
                var progressThread = new ProgressThread(250);
                progressThread.Start("开始提交", "正在提交......");
                try
                {
                    var i = 0;
                    foreach (var item in result)
                    {
                        bool success = true;
                        progressThread.Step = "正在提交......(" + i++ + ")";
                        if (item.State == RecordState.Added || item.State == RecordState.Modified)  //新增或更新
                        {
                            if (item is DictionaryType)
                            {
                                DataServiceManager.DataDictService.saveOrUpdateObject((DictionaryType)item);
                            }
                            else
                            {
                                success = _service.SaveOrUpdateObject(item);
                            }
                            if (success)
                            {
                                item.State = RecordState.Normal;
                            }

                        }
                        else if (item.State == RecordState.Deleted) //删除
                        {
                            success = _service.DeleteObject(item);
                            this._owner.DeleteEntity(item);
                        }


                    }
                }
                finally
                {
                    progressThread.End();
                }
           
            //同步
            SingletonManager.Cache.SaveEntity("系统字典", "系统字典列表", result);
            TaskDialogEx.Information("提交", "内容提交成功!");

            this.Close();
        }

        private void BtnCancelClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
